home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch13 / Reflect.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-20  |  13KB  |  413 lines

  1. VERSION 5.00
  2. Begin VB.Form frmReflect 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Reflect"
  6.    ClientHeight    =   5415
  7.    ClientLeft      =   300
  8.    ClientTop       =   735
  9.    ClientWidth     =   9015
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    PaletteMode     =   1  'UseZOrder
  23.    ScaleHeight     =   5415
  24.    ScaleWidth      =   9015
  25.    Begin VB.PictureBox Pict 
  26.       AutoRedraw      =   -1  'True
  27.       Height          =   2175
  28.       Index           =   7
  29.       Left            =   6840
  30.       ScaleHeight     =   -10
  31.       ScaleLeft       =   -5
  32.       ScaleMode       =   0  'User
  33.       ScaleTop        =   5
  34.       ScaleWidth      =   10
  35.       TabIndex        =   7
  36.       Top             =   2760
  37.       Width           =   2175
  38.    End
  39.    Begin VB.PictureBox Pict 
  40.       AutoRedraw      =   -1  'True
  41.       Height          =   2175
  42.       Index           =   6
  43.       Left            =   4560
  44.       ScaleHeight     =   -10
  45.       ScaleLeft       =   -5
  46.       ScaleMode       =   0  'User
  47.       ScaleTop        =   5
  48.       ScaleWidth      =   10
  49.       TabIndex        =   6
  50.       Top             =   2760
  51.       Width           =   2175
  52.    End
  53.    Begin VB.PictureBox Pict 
  54.       AutoRedraw      =   -1  'True
  55.       Height          =   2175
  56.       Index           =   5
  57.       Left            =   2280
  58.       ScaleHeight     =   -10
  59.       ScaleLeft       =   -5
  60.       ScaleMode       =   0  'User
  61.       ScaleTop        =   5
  62.       ScaleWidth      =   10
  63.       TabIndex        =   5
  64.       Top             =   2760
  65.       Width           =   2175
  66.    End
  67.    Begin VB.PictureBox Pict 
  68.       AutoRedraw      =   -1  'True
  69.       Height          =   2175
  70.       Index           =   4
  71.       Left            =   0
  72.       ScaleHeight     =   -10
  73.       ScaleLeft       =   -5
  74.       ScaleMode       =   0  'User
  75.       ScaleTop        =   5
  76.       ScaleWidth      =   10
  77.       TabIndex        =   4
  78.       Top             =   2760
  79.       Width           =   2175
  80.    End
  81.    Begin VB.PictureBox Pict 
  82.       AutoRedraw      =   -1  'True
  83.       Height          =   2175
  84.       Index           =   3
  85.       Left            =   6840
  86.       ScaleHeight     =   -10
  87.       ScaleLeft       =   -5
  88.       ScaleMode       =   0  'User
  89.       ScaleTop        =   5
  90.       ScaleWidth      =   10
  91.       TabIndex        =   3
  92.       Top             =   0
  93.       Width           =   2175
  94.    End
  95.    Begin VB.PictureBox Pict 
  96.       AutoRedraw      =   -1  'True
  97.       Height          =   2175
  98.       Index           =   2
  99.       Left            =   4560
  100.       ScaleHeight     =   -10
  101.       ScaleLeft       =   -5
  102.       ScaleMode       =   0  'User
  103.       ScaleTop        =   5
  104.       ScaleWidth      =   10
  105.       TabIndex        =   2
  106.       Top             =   0
  107.       Width           =   2175
  108.    End
  109.    Begin VB.PictureBox Pict 
  110.       AutoRedraw      =   -1  'True
  111.       Height          =   2175
  112.       Index           =   1
  113.       Left            =   2280
  114.       ScaleHeight     =   -10
  115.       ScaleLeft       =   -5
  116.       ScaleMode       =   0  'User
  117.       ScaleTop        =   5
  118.       ScaleWidth      =   10
  119.       TabIndex        =   1
  120.       Top             =   0
  121.       Width           =   2175
  122.    End
  123.    Begin VB.PictureBox Pict 
  124.       AutoRedraw      =   -1  'True
  125.       Height          =   2175
  126.       Index           =   0
  127.       Left            =   0
  128.       ScaleHeight     =   -10
  129.       ScaleLeft       =   -5
  130.       ScaleMode       =   0  'User
  131.       ScaleTop        =   5
  132.       ScaleWidth      =   10
  133.       TabIndex        =   0
  134.       Top             =   0
  135.       Width           =   2175
  136.    End
  137.    Begin VB.Label Label1 
  138.       Alignment       =   2  'Center
  139.       Caption         =   "Reverse translation"
  140.       Height          =   255
  141.       Index           =   7
  142.       Left            =   6840
  143.       TabIndex        =   15
  144.       Top             =   5040
  145.       Width           =   2175
  146.       WordWrap        =   -1  'True
  147.    End
  148.    Begin VB.Label Label1 
  149.       Alignment       =   2  'Center
  150.       Caption         =   "Reverse 1st rotation"
  151.       Height          =   255
  152.       Index           =   6
  153.       Left            =   4560
  154.       TabIndex        =   14
  155.       Top             =   5040
  156.       Width           =   2175
  157.       WordWrap        =   -1  'True
  158.    End
  159.    Begin VB.Label Label1 
  160.       Alignment       =   2  'Center
  161.       Caption         =   "Reverse 2nd rotation"
  162.       Height          =   255
  163.       Index           =   5
  164.       Left            =   2280
  165.       TabIndex        =   13
  166.       Top             =   5040
  167.       Width           =   2175
  168.       WordWrap        =   -1  'True
  169.    End
  170.    Begin VB.Label Label1 
  171.       Alignment       =   2  'Center
  172.       Caption         =   "Reflect"
  173.       Height          =   255
  174.       Index           =   4
  175.       Left            =   0
  176.       TabIndex        =   12
  177.       Top             =   5040
  178.       Width           =   2175
  179.       WordWrap        =   -1  'True
  180.    End
  181.    Begin VB.Label Label1 
  182.       Alignment       =   2  'Center
  183.       Caption         =   "Rotate into Y axis"
  184.       Height          =   255
  185.       Index           =   3
  186.       Left            =   6840
  187.       TabIndex        =   11
  188.       Top             =   2280
  189.       Width           =   2175
  190.       WordWrap        =   -1  'True
  191.    End
  192.    Begin VB.Label Label1 
  193.       Alignment       =   2  'Center
  194.       Caption         =   "Rotate into Y-Z plane"
  195.       Height          =   255
  196.       Index           =   2
  197.       Left            =   4560
  198.       TabIndex        =   10
  199.       Top             =   2280
  200.       Width           =   2175
  201.       WordWrap        =   -1  'True
  202.    End
  203.    Begin VB.Label Label1 
  204.       Alignment       =   2  'Center
  205.       Caption         =   "Translate to origin"
  206.       Height          =   255
  207.       Index           =   1
  208.       Left            =   2280
  209.       TabIndex        =   9
  210.       Top             =   2280
  211.       Width           =   2175
  212.       WordWrap        =   -1  'True
  213.    End
  214.    Begin VB.Label Label1 
  215.       Alignment       =   2  'Center
  216.       Caption         =   "Original picture"
  217.       Height          =   255
  218.       Index           =   0
  219.       Left            =   0
  220.       TabIndex        =   8
  221.       Top             =   2280
  222.       Width           =   2175
  223.       WordWrap        =   -1  'True
  224.    End
  225. Attribute VB_Name = "frmReflect"
  226. Attribute VB_GlobalNameSpace = False
  227. Attribute VB_Creatable = False
  228. Attribute VB_PredeclaredId = True
  229. Attribute VB_Exposed = False
  230. Option Explicit
  231. ' The plane across which to reflect.
  232. Private Const Px = 1
  233. Private Const Py = 0
  234. Private Const Pz = 0
  235. Private Const Dx = 1
  236. Private Const Dy = 1
  237. Private Const Dz = 1
  238. ' Location of viewing eye.
  239. Private EyeR As Single
  240. Private EyeTheta As Single
  241. Private EyePhi As Single
  242. ' Location of focus point.
  243. Private Const FocusX = 0#
  244. Private Const FocusY = 0#
  245. Private Const FocusZ = 0#
  246. Private Projector(1 To 4, 1 To 4) As Single
  247. ' Matrices used for the reflection.
  248. Private trans(1 To 4, 1 To 4) As Single
  249. Private Rot1(1 To 4, 1 To 4) As Single
  250. Private Rot2(1 To 4, 1 To 4) As Single
  251. Private Ref(1 To 4, 1 To 4) As Single
  252. Private Rot2_Inv(1 To 4, 1 To 4) As Single
  253. Private Rot1_Inv(1 To 4, 1 To 4) As Single
  254. Private Trans_Inv(1 To 4, 1 To 4) As Single
  255. Private M(0 To 7) As Transformation
  256. ' Create the matrices used when reflecting across
  257. ' a plane passing through (p1, p2, p3) with
  258. ' normal <n1, n2, n3>.
  259. Private Sub CreateMatrices(ByVal p1 As Single, ByVal p2 As Single, ByVal p3 As Single, ByVal n1 As Single, ByVal n2 As Single, ByVal n3 As Single)
  260. Dim D As Single
  261. Dim L As Single
  262.     ' Translate the plane to the origin.
  263.     m3Translate trans, -p1, -p2, -p3
  264.     m3Translate Trans_Inv, p1, p2, p3
  265.     ' Rotate around Z-axis until the normal is in
  266.     ' the Y-Z plane.
  267.     m3Identity Rot1
  268.     D = Sqr(n1 * n1 + n2 * n2)
  269.     Rot1(1, 1) = n2 / D
  270.     Rot1(1, 2) = n1 / D
  271.     Rot1(2, 1) = -Rot1(1, 2)
  272.     Rot1(2, 2) = Rot1(1, 1)
  273.     m3Identity Rot1_Inv
  274.     Rot1_Inv(1, 1) = Rot1(1, 1)
  275.     Rot1_Inv(1, 2) = -Rot1(1, 2)
  276.     Rot1_Inv(2, 1) = -Rot1(2, 1)
  277.     Rot1_Inv(2, 2) = Rot1(2, 2)
  278.     ' Rotate around the X-axis until the normal
  279.     ' lies along the Y axis.
  280.     m3Identity Rot2
  281.     L = Sqr(n1 * n1 + n2 * n2 + n3 * n3)
  282.     Rot2(2, 2) = D / L
  283.     Rot2(2, 3) = -n3 / L
  284.     Rot2(3, 2) = -Rot2(2, 3)
  285.     Rot2(3, 3) = Rot2(2, 2)
  286.     m3Identity Rot2_Inv
  287.     Rot2_Inv(2, 2) = Rot2(2, 2)
  288.     Rot2_Inv(2, 3) = -Rot2(2, 3)
  289.     Rot2_Inv(3, 2) = -Rot2(3, 2)
  290.     Rot2_Inv(3, 3) = Rot2(3, 3)
  291.     ' Reflect across the X-Z plane.
  292.     m3Identity Ref
  293.     Ref(2, 2) = -1
  294.     ' Put the matrices in the M array.
  295.     m3Identity M(0).M
  296.     m3MatCopy M(1).M, trans
  297.     m3MatCopy M(2).M, Rot1
  298.     m3MatCopy M(3).M, Rot2
  299.     m3MatCopy M(4).M, Ref
  300.     m3MatCopy M(5).M, Rot2_Inv
  301.     m3MatCopy M(6).M, Rot1_Inv
  302.     m3MatCopy M(7).M, Trans_Inv
  303. End Sub
  304. ' Let the user change the location of the eye.
  305. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  306. Const Dtheta = PI / 20
  307.     Select Case KeyCode
  308.         Case vbKeyLeft
  309.             EyeTheta = EyeTheta - Dtheta
  310.             
  311.         Case vbKeyRight
  312.             EyeTheta = EyeTheta + Dtheta
  313.         
  314.         Case vbKeyUp
  315.             EyePhi = EyePhi - Dtheta
  316.         
  317.         Case vbKeyDown
  318.             EyePhi = EyePhi + Dtheta
  319.         
  320.         Case Else
  321.             Exit Sub
  322.     End Select
  323.     ' Redraw the pictures.
  324.     DrawTheData
  325. End Sub
  326. ' Rotate the points in the cube and draw the cube.
  327. Private Sub DrawData(ByVal pic As Object)
  328. Dim i As Integer
  329. Dim x1 As Single
  330. Dim y1 As Single
  331. Dim x2 As Single
  332. Dim y2 As Single
  333. Dim oldwidth As Integer
  334.     ' Compute the projection matrix.
  335.     m3PProject Projector, m3Parallel, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  336.     ' Transform the points.
  337.     TransformAllData Projector
  338.     ' Draw the points.
  339.     pic.Cls
  340.     oldwidth = pic.DrawWidth
  341.     For i = 1 To NumSegments
  342.         x1 = Segments(i).fr_tr(1)
  343.         y1 = Segments(i).fr_tr(2)
  344.         x2 = Segments(i).to_tr(1)
  345.         y2 = Segments(i).to_tr(2)
  346.         
  347.         ' Draw the plane's normal in bold.
  348.         If i = 4 Then pic.DrawWidth = 3
  349.         pic.Line (x1, y1)-(x2, y2)
  350.         If i = 4 Then pic.DrawWidth = oldwidth
  351.     Next i
  352.     pic.Refresh
  353. End Sub
  354. Private Sub Form_Load()
  355.     ' Initialize the eye position.
  356.     EyeR = 3
  357.     EyeTheta = PI * 0.4
  358.     EyePhi = PI * 0.1
  359.     ' Create the reflection matrices.
  360.     CreateMatrices Px, Py, Pz, Dx, Dy, Dz
  361.     ' Create, project, and draw the data.
  362.     DrawTheData
  363. End Sub
  364. ' Draw all the objects.
  365. Private Sub DrawTheData()
  366. Dim i As Integer
  367.     ' Start with fresh data.
  368.     CreateData
  369.     For i = 0 To 7
  370.         ' Apply the next transformation.
  371.         TransformData M(i).M, 4, NumSegments
  372.         SetPoints 4, NumSegments
  373.         
  374.         ' Display the data.
  375.         DrawData Pict(i)
  376.     Next i
  377. End Sub
  378. ' Create the data.
  379. Private Sub CreateData()
  380.     ' Start with no data.
  381.     NumSegments = 0
  382.     ' Create the axes.
  383.     MakeSegment 0, 0, 0, 5, 0, 0    ' X axis.
  384.     MakeSegment 0, 0, 0, 0, 5, 0    ' Y axis.
  385.     MakeSegment 0, 0, 0, 0, 0, 5    ' Z axis.
  386.     ' Create the reflection plane's normal.
  387.     MakeSegment Px, Py, Pz, Px + Dx, Py + Dy, Pz + Dz
  388.     ' Create the edges of the reflection plane.
  389.     MakeSegment 0.5, -2, 2.5, 3.5, -2, -0.5
  390.     MakeSegment 3.5, -2, -0.5, 1.5, 2, -2.5
  391.     MakeSegment 1.5, 2, -2.5, -1.5, 2, 0.5
  392.     MakeSegment -1.5, 2, 0.5, 0.5, -2, 2.5
  393.     ' Create the object to reflect.
  394.     MakeSegment 3, 1, 2, 3, 3, 2
  395.     MakeSegment 3, 3, 2, 4, 3, 2
  396.     MakeSegment 4, 3, 2, 4, 2, 2
  397.     MakeSegment 4, 2, 2, 5, 2, 2
  398.     MakeSegment 5, 2, 2, 5, 1, 2
  399.     MakeSegment 5, 1, 2, 3, 1, 2
  400.     MakeSegment 3, 1, 1, 3, 3, 1
  401.     MakeSegment 3, 3, 1, 4, 3, 1
  402.     MakeSegment 4, 3, 1, 4, 2, 1
  403.     MakeSegment 4, 2, 1, 5, 2, 1
  404.     MakeSegment 5, 2, 1, 5, 1, 1
  405.     MakeSegment 5, 1, 1, 3, 1, 1
  406.     MakeSegment 3, 1, 1, 3, 1, 2
  407.     MakeSegment 3, 3, 1, 3, 3, 2
  408.     MakeSegment 4, 3, 1, 4, 3, 2
  409.     MakeSegment 4, 2, 1, 4, 2, 2
  410.     MakeSegment 5, 2, 1, 5, 2, 2
  411.     MakeSegment 5, 1, 1, 5, 1, 2
  412. End Sub
  413.